package com.yishan.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.yishan.domain.Role;
import com.yishan.service.ModuleService;
import com.yishan.service.RoleService;
import com.yishan.utils.BeanUtil;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @Author yishan
 * @Date 2021/4/28 0028 1:20
 * @Version 1.0
 */
@Controller
@RequestMapping("/role")
public class RoleController {

	@Autowired
	private RoleService roleService;

	@Autowired
	private ModuleService moduleService;

	/**
	 * 查询所有
	 *
	 * @param model
	 * @param request
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("/list")
	private String list(Model model, HttpServletRequest request)
			throws ServletException, IOException {

		int page = 1;
		int size = 5;

		if (StringUtils.isNotBlank(request.getParameter("page"))) {
			page = Integer.parseInt(request.getParameter("page"));
		}
		if (StringUtils.isNotBlank(request.getParameter("size"))) {
			size = Integer.parseInt(request.getParameter("size"));
		}

		PageInfo info = roleService.findAll(page, size);

		model.addAttribute("page", info);

		return "/WEB-INF/pages/role/list.jsp";

	}

	/**
	 * 跳转到添加页面
	 *
	 * @return
	 */
	@RequestMapping("/toAdd")
	private String toAdd() {

		return "/WEB-INF/pages/role/add.jsp";

	}

	/**
	 * 添加操作
	 *
	 * @param request
	 * @return
	 */
	@RequestMapping("/save")
	private String save(HttpServletRequest request) {

		Role role = BeanUtil.fillBean(request, Role.class);
		roleService.save(role);

		return "redirect:" + request.getContextPath() + "/role/list";

	}

	/**
	 * 跳转到修改页面
	 *
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping("/toEdit")
	private String toEdit(String id, Model model) {

		Role role = roleService.findById(id);

		model.addAttribute("role", role);

		return "/WEB-INF/pages/role/update.jsp";

	}

	/**
	 * 修改操作
	 *
	 * @param role
	 * @return
	 */
	@RequestMapping("/edit")
	private String edit(Role role) {

		roleService.update(role);

		return "redirect:/role/list";

	}

	/**
	 * 删除操作
	 *
	 * @param id
	 * @return
	 */
	@RequestMapping("/delete")
	private String delete(String ids) {
		System.out.println(ids);

		roleService.deleteMany(ids);

		return "redirect:/role/list";

	}

	@RequestMapping("/author")
	private String author(@RequestParam("id") String roleId,Model model) throws JsonProcessingException {

		//获取需要授权的角色
		Role role = roleService.findById(roleId);
		model.addAttribute("role",role);

		//根据角色id获取所有的模块数据，并加载关系数据
		List<Map> map = moduleService.findAuthorDataByRoleId(roleId);

		//map转化为json
		ObjectMapper objectMapper = new ObjectMapper();
		String json = objectMapper.writeValueAsString(map);

		model.addAttribute("roleModuleJson",json);

		return "/WEB-INF/pages/role/author.jsp";

	}

	@RequestMapping("/updateRoleModule")
	public String updateRoleModule(String roleId,String moduleIds){

		roleService.updateRoleModule(roleId,moduleIds);

		return "redirect:/role/list";
	}

}
